// packageA/pages/choosLoction/choosLoction.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexList: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    serchHotHeight: 0,
    cityinputvalue: '',
    mapKey: 'e24353dac4e7753f2eea4ce5e3112b2d'
  },

  // 定位
  getAddr: function () {
    var that = this
    wx.getSetting({
      success(res) {
        // 如果已拒绝授权，则打开设置页面
        if (res.authSetting['scope.userLocation'] == false) {
          that.setData({
            noOpenLocate: true
          })
          wx.openSetting({
            success(res) {
              console.log('settingres==', res)
              // 如果授权位置, 重新获取当前位置
              if (res.authSetting['scope.userLocation'] == true) {
                that.setData({
                  noOpenLocate: false
                })
                that.getMyLocation()
              } else {
                wx.showToast({
                  title: '授权失败',
                  icon: 'error'
                })
              }

            }
          })
        } else {
          that.getMyLocation()
        }
      }
    })

  },
  getMyLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        that.getweizhi(latitude, longitude)
      }
    })
  },
  // 获取位置
  getweizhi: function (latitude, longitude) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    // https://restapi.amap.com/v3/geocode/regeo?output=xml&location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all 
    wx.request({
      url: `https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=${longitude},${latitude}&key=e24353dac4e7753f2eea4ce5e3112b2d`,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (result) {
        wx.hideLoading()
        that.setData({
          provName: result.data.regeocode.addressComponent.province,
          cityName: result.data.regeocode.addressComponent.city
        })
      },
      fail: function (res) {
        // that.getFJ_Get_RecListKey();
      }
    })
    // wx.request({
    //   url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${that.data.mapKey}`,
    //   header: {
    //     'Content-Type': 'application/json'
    //   },
    //   success: function (result) {
    //     wx.hideLoading()
    //     console.log('result==', result)
    //     that.setData({
    //       provName: result.data.result.ad_info.province,
    //       cityName: result.data.result.ad_info.city
    //     })
    //   },
    //   fail: function (res) {
    //     // that.getFJ_Get_RecListKey();
    //   }
    // })
  },
  // 重新定位
  againLocation: function () {
    this.getAddr()
  },

  setOffsetTop() {
    wx.createSelectorQuery()
      .select('.position')
      .fields(
        { size: true },
        ({ height }) => this.setData({ offsetTop: height })
      )
      .exec()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryCtiyList()
    // 获取当前位置
    this.getAddr()
    // 获取元素距离顶部高度
    this.setOffsetTop()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取当前位置
    this.getAddr()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  queryCtiyList(strKey = '') {
    wx.showLoading({
      title: '数据加载中',
    })
    let input = {
      strKey: strKey
    },
      that = this
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_Index_CityList', input, r => {
      console.log(r, '城市列表')
      wx.hideLoading({
        success: (res) => { },
      })
      if (r.code == 200) {
        let indexList = []
        r.data.CityList.forEach(item => {
          indexList.push(item.szm)
        })
        that.setData({
          indexList,
          CityList: r.data.CityList,
          HotCity: r.data.HotCity
        }, () => {
          const query = wx.createSelectorQuery()
          query.select(".position")
            .boundingClientRect((rect) => {
              let height = rect.height;
              console.log("height: ", height, rect);
              that.setData({
                serchHotHeight: height
              })
            })
            .exec();
        })
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  selectCity(e) {
    let city = e.currentTarget.dataset.city
    wx.setStorageSync('isCity', city)
    var pages = getCurrentPages();//当前页页面实例
    var prevPage = pages[pages.length - 2];//上一页页面实例
    if (prevPage) {
      //给上一页面的变量赋值
      prevPage.setData({
        city: city,
        isCityBack: true,
      })
      //返回上一级
      wx.navigateBack({
        delta: 1,
      })
    }
  },
  inputcity(e) {
    // this.setData({
    //   cityinputvalue:e.detail.value
    // },()=>{
    this.queryCtiyList(e.detail.value)
    // })
  }

})